<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serveType">
              <j-dict-select-tag type="list" v-model="model.serveType" dictCode="serve_type" placeholder="请选择服务类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入服务名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24" v-if="title!='新增'">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="radioButton" v-model="model.status" dictCode="serve_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tagsId">
              <a-input v-model="model.tagsId" placeholder="请输入标签"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="底部图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imagesBottom">
              <j-image-upload biz-path="laile2/serve" isMultiple :number=9 v-model="model.imagesBottom" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imagesCarousel">
              <j-image-upload biz-path="laile2/serve" isMultiple :number=9 v-model="model.imagesCarousel" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="person">
              <a-input v-model="model.person" placeholder="请输入联系人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contact">
              <a-input v-model="model.contact" placeholder="请输入联系方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detail">
              <a-textarea
                v-model="model.detail"
                placeholder="请输入详情"
                :auto-size="{ minRows: 3, maxRows: 5 }"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ServeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: String,
    },
    data () {
      return {
        model:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           serveType: [
              { required: true, message: '请选择服务类型!'},
           ],
           name: [
              { required: true, message: '请输入服务名称!'},
           ],
          //  status: [
          //     { required: true, message: '请选择状态!'},
          //  ],
        },
        url: {
          add: "/serve/add",
          edit: "/serve/edit",
        },
      }
    },
    computed: {
      formDisabled () {
        return this.disabled
      },
      ...mapGetters([
        "userInfoSite",
        "userInfoMerchant",
        "userInfoCurrentShop",
      ]),
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.$emit('handleChangeThreeElements', {
          site: this.userInfoSite ? this.userInfoSite['name'] : null,
          merchant: this.userInfoMerchant ? this.userInfoMerchant['name'] : null,
          shop: this.userInfoCurrentShop ? this.userInfoCurrentShop['name'] : null
        })
        this.visible = true;
      },
      edit (record) {
        this.$emit('handleChangeThreeElements', {
          site: record['siteId_dictText'],
          merchant: record['merchantId_dictText'],
          shop: record['shopId_dictText']
        })
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            console.log(this.model)
            /*if (this.model.imagesBottom && typeof this.model.imagesBottom !== 'string') {
              this.model.imagesBottomString = this.model.imagesBottom.join(',')
              this.model.imagesBottom = null
            } else {
              this.model.imagesBottomString = this.model.imagesBottom
              this.model.imagesBottom = null
            }
            if (this.model.imagesCarousel && typeof this.model.imagesCarousel !== 'string') {
              this.model.imagesCarouselString = this.model.imagesCarousel.join(',')
              this.model.imagesCarousel = null
            } else {
              this.model.imagesCarouselString = this.model.imagesCarousel
              this.model.imagesCarousel = null
            }*/
            if (this.model.imagesBottom && this.model.imagesBottom.length > 0 && typeof this.model.imagesBottom === 'string') {
              this.model.imagesBottom = this.model.imagesBottom.split(',')
            }
            if (this.model.imagesCarousel && this.model.imagesCarousel.length > 0 && typeof this.model.imagesCarousel === 'string') {
              this.model.imagesCarousel = this.model.imagesCarousel.split(',')
            }
            console.log(this.model)
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>

<style scoped>
  .avatar-uploader > .ant-upload {
    width:104px;
    height:104px;
  }
</style>